package com.hs.jhzd.controller;

import com.hs.jhzd.entity.Prisoner;
import com.hs.jhzd.entity.PrisonerPhoto;
import com.hs.jhzd.repository.ConfigsRepository;
import com.hs.jhzd.repository.PrisonerPhotoRepository;
import com.hs.jhzd.repository.PrisonerRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/p")
@Slf4j
public class PrisonerController {

    @Autowired
    private PrisonerRepository repository;

    @Autowired
    private PrisonerPhotoRepository prisonerPhotoRepository;

    @Autowired
    private ConfigsRepository configsRepository;

    @RequestMapping({"/findPhotoByRybh", "/findPhotoByRybh.jpg"})
    @ResponseBody
    public byte[] findPhotoByRybh(HttpServletRequest req, HttpServletResponse resp, String rybh) {
        resp.setContentType(MediaType.IMAGE_JPEG_VALUE);

        PrisonerPhoto photo = prisonerPhotoRepository.findByRybh(rybh);
        return photo.getZm();
//        List<Configs> configs = configsRepository.findAll();
//        if (CollectionUtils.isEmpty(configs)) {
//            return null;
//        }
//        String hongsenApi = configs.get(0).getHongsenApi();
//        if (StringUtils.isBlank(hongsenApi)) {
//            return null;
//        }
//        if (!StringUtils.endsWith(hongsenApi, "/")) {
//            hongsenApi += "/";
//        }
//
//        OkHttpClient client = new OkHttpClient();
//
//        // aimType=0在押人员，1民警
//        String url = hongsenApi + "getImgFile?aimType=0&rybh=" + rybh;
//        log.info("【访问实战平台获取人员照片】完整地址：{}", url);
//        Request request = new Request.Builder().url(url).get().build();
//
//        try {
//            Response response = client.newCall(request).execute();
//            return response.body().bytes();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return null;
    }

    @RequestMapping("/findByJsbh")
    @ResponseBody
    public List<Prisoner> findByJsbh(String jsbh) {
        return repository.findAll();
    }

    @RequestMapping("/findByJsh")
    @ResponseBody
    public List<Prisoner> findBfindByJshyJsbh(String jsh,String jsbh) {
        return repository.findByJshAndRybjAndJsbh(jsh, "3",jsbh);
    }

    @RequestMapping("/findByRybh")
    @ResponseBody
    public Prisoner findByRybh(String rybh) {
        return repository.findByRybh(rybh);
    }

    @RequestMapping("/findByCardId")
    @ResponseBody
    public Prisoner findByCardId(String jsh, String cardId) {
        return repository.findByJshAndCardIdAndRybj(jsh, cardId, "3");
    }

    @RequestMapping("/findByFh")
    @ResponseBody
    public Prisoner findByFh(String fh) {
        return repository.findByFh(fh);
    }

    @RequestMapping("/findByName")
    @ResponseBody
    public List<Prisoner> findByName(String name) {
        return repository.findByName(name);
    }

}
